package com.yyfax.pay.config.data.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.framework.exception.YYSqlException;
import com.yyfax.framework.sql.SQLFactory;
import com.yyfax.pay.config.data.dao.BankConfigDao;
import com.yyfax.pay.config.model.po.BankConfigPO;

@Repository
public class BankConfigDaoImpl extends BaseDaoImpl<BankConfigPO, Integer> implements BankConfigDao {

	// 这里写死,将来可以考虑注入
	private static final String TABLE_NAME = "bank_config";

	private static final String SQL_INSERT = "insert";

	private static final String SQL_UPDATE = "update";

	private static final String SQL_SELECT_FOR_LIST = "selectForList";
	/** 根据渠道bankCode查询 */
	private static final String SQL_SELECT_BY_CONFIG = "selectByBankCode";
	/** 根据渠道ID查询 */
	private static final String SQL_SELECT_BY_ID = "selectById";
	/** 根据支付渠道编号和银行编号查询 */
	// private static final String SQL_SELECT_BY_THIRDBANKCODE = "selectByThirdBankCode";

	@Autowired
	private SQLFactory sqlFactory;

	public BankConfigDaoImpl() {
		super(TABLE_NAME);
	}

	@Override
	public int insert(BankConfigPO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);

		return namedParameterJdbcTemplate.insertForPK(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public int update(BankConfigPO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);
		return namedParameterJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public BankConfigPO selectByBankCode(String bankCode) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_CONFIG);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("bankCode", bankCode);

		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<BankConfigPO>(BankConfigPO.class));
	}

	@Override
	public List<BankConfigPO> selectForList() throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_FOR_LIST);
		return namedParameterJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<BankConfigPO>(BankConfigPO.class));
	}

	@Override
	public BankConfigPO selectById(int id) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id", id);

		return namedParameterJdbcTemplate.queryForObject(sql.toString(), params, new BeanPropertyRowMapper<BankConfigPO>(BankConfigPO.class));
	}

	// @Override
	// public BankConfigPO seletByThirdBankCode(String paymentCode, String thirdBankCode) throws Exception {
	// String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_THIRDBANKCODE);
	//
	// sql = sqlFactory.processSql(sql, "#condition#", paymentCode);
	//
	// MapSqlParameterSource params = new MapSqlParameterSource();
	// params.addValue("paymentCode", paymentCode);
	// params.addValue("thirdBankCode", thirdBankCode);
	//
	// return namedParameterJdbcTemplate.queryForObject(sql.toString(), params, new BeanPropertyRowMapper<BankConfigPO>(BankConfigPO.class));
	// }
}
